package gaej.example.contact.client;

import java.util.List;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HTMLTable.Cell;

public class ComponistGUI {
    /* Constants. */
    private static final String LISTING_ROOT_PANEL = "guiListing";
    private static final String FORM_ROOT_PANEL = "guiForm";
    private static final String STATUS_ROOT_PANEL = "guiStatus";
    private static final String TOOL_BAR_ROOT_PANEL = "guiToolBar";
    private static final int EDIT_LINK = 3;
    private static final int REMOVE_LINK = 4;

    /* GUI Widgets */
    protected Button addButton;
    protected Button updateButton;
    protected Button addNewButton;
    protected TextBox naamField;
    protected TextBox landField;
    protected TextBox tijdvakField;
    protected TextBox compositiesField;
    protected Label status;
    protected Grid listGrid;
    protected Grid formGrid;
    
    /* Data model */
    private List<Componist> componisten;
    private Componist currentComponist;
    private Persoon currentPersoon;
    protected ComponistDelegate componistService;
        
    public void init() {
        addButton = new Button("Add componist");
        addNewButton = new Button("Add new componist");
        updateButton = new Button("Update componist");
        naamField = new TextBox();
        landField = new TextBox();
        tijdvakField = new TextBox();
        compositiesField = new TextBox();
        status = new Label();
        listGrid = new Grid(2,6);

        buildForm();
        placeWidgets();
    }
    
    private void buildForm() {
        formGrid = new Grid(5,3);
        formGrid.setVisible(false);
        
        formGrid.setWidget(0, 0, new Label("Naam"));
        formGrid.setWidget(0, 1, naamField);

        formGrid.setWidget(1, 0, new Label("Land"));
        formGrid.setWidget(1, 1, landField);
        
        formGrid.setWidget(2, 0, new Label("Tijdvak"));
        formGrid.setWidget(2, 1, tijdvakField);

        formGrid.setWidget(3, 0, new Label("Composities"));
        formGrid.setWidget(3, 1, compositiesField);
        
        formGrid.setWidget(4, 0, updateButton);
        formGrid.setWidget(4, 1, addButton);
        
    }

    private void placeWidgets() {
        RootPanel.get(LISTING_ROOT_PANEL).add(listGrid);
        RootPanel.get(FORM_ROOT_PANEL).add(formGrid);
        RootPanel.get(STATUS_ROOT_PANEL).add(status);
        RootPanel.get(TOOL_BAR_ROOT_PANEL).add(addNewButton);
    }

    public void hideGui() {
    	formGrid.setVisible(false);
    	listGrid.setVisible(false);
    	status.setVisible(false);
    	addNewButton.setVisible(false);
    }

    
    private void loadForm(Componist componist) {
        this.formGrid.setVisible(true);
        currentComponist = componist;
        currentPersoon = componist.getPersoon();
        this.naamField.setText(currentPersoon.getNaam());
        this.landField.setText(currentPersoon.getLand());
        this.tijdvakField.setText(componist.getTijdvak());
        this.compositiesField.setText(componist.getComposities().toString());
    }


    private void copyFieldDateToComponist() {
        currentPersoon.setNaam(naamField.getText());
        currentPersoon.setLand(landField.getText());
        currentComponist.setPersoon(currentPersoon);
        currentComponist.setTijdvak(tijdvakField.getText());
        currentComponist.setComposities(Long.parseLong(compositiesField.getText()));
    }

    public void gui_eventListGridClicked(Cell cellClicked) {
         int row = cellClicked.getRowIndex();
         int col = cellClicked.getCellIndex();
        
         Componist componist = this.componisten.get(row);
         Persoon persoon = componist.getPersoon();
         this.status.setText("Naam was " + persoon.getNaam() + " clicked ");
        
         if (col==EDIT_LINK) {
             this.addNewButton.setVisible(false);
             this.updateButton.setVisible(true);
             this.addButton.setVisible(false);
             loadForm(componist);
         } else if (col==REMOVE_LINK) {
        	 if (Window.confirm("Verwijderen componist ?")) {
        		 this.componistService.removeComponist(componist);
        	 }
         }
    }


    public void gui_eventAddButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToComponist();
        this.componistService.addComponist(currentComponist);
    }

    public void gui_eventUpdateButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToComponist();
        this.componistService.updateComponist(currentComponist);
    }

    public void gui_eventAddNewButtonClicked() {
        this.addNewButton.setVisible(false);
        this.updateButton.setVisible(false);
        this.addButton.setVisible(true);
        Componist componist = new Componist();
        Persoon persoon = new Persoon();
        componist.setPersoon(persoon);
        componist.setComposities(0L);
        loadForm(componist);
    }


    public void service_eventListRetrievedFromService(List<Componist> result) {
        status.setText("Retrieved componist list");
        status.setVisible(true);

        //formGrid.setVisible(false);
    	listGrid.setVisible(true);
    	status.setVisible(true);
    	addNewButton.setVisible(true);
        
        this.componisten = result;
        this.listGrid.clear();
        this.listGrid.resizeRows(this.componisten.size());
        int row = 0;
        this.addNewButton.setVisible(true);
        
        for (Componist componist : result) {
        	Persoon persoon = componist.getPersoon();
        	if (persoon == null) {
        		persoon = new Persoon("--","--");
        	}
        	this.listGrid.setWidget(row, 0, new Label (persoon.getNaam()));
            this.listGrid.setWidget(row, 1, new Label (persoon.getLand()));
            this.listGrid.setWidget(row, 2, new Label (componist.getTijdvak()));
            this.listGrid.setWidget(row, 3, new Label (componist.getComposities().toString()));
            this.listGrid.setWidget(row, EDIT_LINK, new Hyperlink("Edit", null));
            this.listGrid.setWidget(row, REMOVE_LINK, new Hyperlink("Remove", null));
            row ++;
        }
    }

    public void service_eventAddComponistSuccessful() {
        status.setText("Componist was successfully added");
        this.componistService.listComponisten();
    }

    public void service_eventUpdateSuccessful() {
        status.setText("Componist was successfully updated");
        this.componistService.listComponisten();
    }
    public void service_eventRemoveComponistSuccessful() {
        status.setText("Componist was removed");
        this.componistService.listComponisten();
        
    }

    public void service_eventUpdateComponistFailed(Throwable caught) {
        status.setText("Update componist failed");
    }

    public void service_eventAddComponistFailed(Throwable caught) {
        status.setText("Unable to update componist");
    }

    public void service_eventRemoveComponistFailed(Throwable caught) {
        status.setText("Remove componist failed");
    }

    public void service_eventListComponistenFailed(Throwable caught) {
        status.setText("Unable to get componist list");
        
    }

}
